package com.nchu.service.impl;

import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nchu.common.util.PageResult;
import com.nchu.entity.OrderDetail;
import com.nchu.entity.Orders;
import com.nchu.entity.User;
import com.nchu.mapper.OrderMapper;
import com.nchu.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.mockito.internal.matchers.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yaojie
 * @since 2025-01-09
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements IOrderService {
    @Autowired
    OrderDetailServiceImpl orderDetailService;
    UserServiceImpl userService;
    @Override
    public PageResult<Orders> findByPage(Integer page, Integer pageSize) {
        //调用 MyBatis-Plus 的分页查询方法，查询 Order 表的分页数据
        Page<Orders> pg = this.page(new Page<>(page, pageSize));

        //获取查询到的所有订单记录
        List<Orders> orders = pg.getRecords();
        //遍历每个订单，根据订单 ID 查询对应的订单详情
        for (Orders order : orders) {
            // 使用 OrderDetailService 查询与当前 Order 相关的 OrderDetail 列表
            List<OrderDetail> orderDetails = orderDetailService.getOrderDetailsByOrderId(order.getId());

            // 将查询到的订单详情列表设置到 Order 的 orderDetailList 属性中
            order.setOrderDetail(orderDetails);
        }
        // 返回分页结果，包含每个 Order 对象的 orderDetailList
        return new PageResult<>(orders, pg.getTotal());
    }

    @Override
    public boolean addOrder(Orders orders) {
        return this.save(orders);
    }
}
